﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ShowFPS : MonoBehaviour {

    public float f_UpdateInterval = 0.5F;

    private float f_LastInterval;

    private int i_Frames = 0;

    private float f_Fps;
	Text _text;
    void Start()
    {
		//Application.targetFrameRate=60;
		_text = GetComponentInChildren<Text>(true);
		if (_text == null)
		{
			Debug.LogErrorFormat("ShowFPS::Start(): can nont find text component");
			this.enabled = false;
			return;
		}

        f_LastInterval = Time.realtimeSinceStartup;

        i_Frames = 0;
    }

    //void OnGUI()
    //{
    //    GUI.Label(new Rect(Screen.width - 40, 1, 200, 200), f_Fps.ToString("f2"));
    //}

    void Update()
    {
        ++i_Frames;

        if (Time.realtimeSinceStartup > f_LastInterval + f_UpdateInterval)
        {
            f_Fps = i_Frames / (Time.realtimeSinceStartup - f_LastInterval);

            i_Frames = 0;

            f_LastInterval = Time.realtimeSinceStartup;
			_text.text = f_Fps.ToString("f2");

		}
    }
}